<?php 
	include("library/vars.php");
	include("library/header.php"); 

	if ($show != null) { // If a Show is selected...
		// Display the Show's Seasons
		include("library/config.php");
		include("library/opendb.php");
		
		$query="SELECT id, show_ID, season FROM seasons WHERE show_ID='$show' ORDER BY season ASC";
		$result = mysql_query($query);
		$has_show=mysql_numrows($result);
		
		if ($has_show > 0) {	
	
			echo '<div id="main">
				<div id="seasons">
				<ul>
				';
				while(list($season_id,$show_id,$season_name)= mysql_fetch_row($result)) {
					echo '<li';
					if ($season == $season_name) {
						echo ' class="selected"';
					}
					echo '><a href="?s=' . $show_id . '&amp;t=' . $season_name . '">'._("Season").' ' . $season_name . '</a></li>
					';
				}
	
			// Add New Season link
			echo '<li id="add_season"><a href="add_season.php?s=' . $show . '">'._("Add New Season").'</a></li>
			</ul>
			<div class="clear"></div>
			</div>
			';
	
			// Success and Error Messages
			echo '<div id="messages">';
				include ("library/messages.php");
			echo '</div>
			';	
		}
		
		else {
			echo '<div id="main_home">';
		}
	
		mysql_free_result($result);
		include("library/closedb.php");
	}

	else { // If a Show is not selected
		
		include("library/config.php");
		include("library/opendb.php");
	
		$query="SELECT show_name,show_nicename, path FROM shows ORDER BY show_name ASC";
		$result = mysql_query($query);
		$totalshows=mysql_numrows($result);	

		// Display the artwork of all shows available in the home screen
		echo '<div id="main_home">
		<!--[if lt IE 7]>
			<div id="ifie">
				<p><strong>'. _("For Internet Explorer 6 users:").'</strong></p>
				<p>'. _("This website is not ment to be used with Internet Explorer 6 or lower.<br />Please update to <a href='http://www.microsoft.com/windows/products/winfamily/ie/default.mspx'>Internet Explorer 7</a> or use another browser like <a href='http://getfirefox.com'>Firefox</a>, <a href='http://opera.com'>Opera</a> or <a href='http://apple.com/safari'>Safari</a> instead to fully enjoy this website.").'</p>
				<p>'. _("You may still use Internet Explorer 6, but some features may not work for you or the layout may be displaced in some areas.").'</p>
			</div>
		<![endif]-->';
		
		echo '<div id="home_screen">
		';
		// Success and Error Messages
		echo '<div id="messages">
		';
			include ("library/messages.php");
		echo '</div>
		';	
	
		if ($totalshows > 0) {
			echo '<ul>
					';
				while(list($show_name,$show_nicename,$path)= mysql_fetch_row($result)) {
					if ($path != null) {
						echo '<li><a href="index.php?s=' . $show_nicename . '">' . $show_name . '<br/><img class="reflect" src="' . "$path" . '" alt="' . "$show_name" . '" width="155"/></a></li>
							';
					}
					else {
						echo '<li><a href="index.php?s=' . $show_nicename . '">' . $show_name . '<br/><img class="reflect" src="library/images/no_artwork.png" alt="' . "$show_name" . '" width="155"/></a></li>
							';	
					}
				}
				echo '</ul>';
			}
		else {
			echo '<p class="erro">'._("You don't have any Shows yet!").'<br /><a href="add_show.php">'._("Would you like to add one now?").'</a></p>';
		}
		echo '</div>
		';
	
		mysql_free_result($result);
		include("library/closedb.php"); 
	}

	if ($show != null) { // If a Show is Selected... display it's details
	
		include("library/config.php");
		include("library/opendb.php");

		$query="SELECT * FROM shows WHERE show_nicename='$show'";
		$result=mysql_query($query);
		$has_show=mysql_numrows($result);
		
		if ($has_show > 0) {	

		$i=0;
		$show_ID=mysql_result($result,$i,"id");
		$show_name=mysql_result($result,$i,"show_name");
		$cast=mysql_result($result,$i,"cast");
		$path=mysql_result($result,$i,"path");
		$size=mysql_result($result,$i,"size");
		
		$query2="SELECT * FROM seasons WHERE show_ID='$show' AND season='$season'";
		$result2 = mysql_query($query2);
		$has_season=mysql_numrows($result2);

		echo'<h2>' .$show_name. '</h2>
			<div id="show">';
		
			// Check to see if the Show has artwork
			if ($size == 0) {
				echo '<img class="reflect" src="library/images/no_artwork.png" alt="' . "$show_name" . '" width="180"/>';
			} 
			else {
				echo '<img class="reflect" src="' . "$path" . '" alt="' . "$show_name" . '" width="180"/>';
			}
			
			// Menu with the Show/Season options
			echo '<div id="show_options">';
			
				if ($has_season > 0) { // If a Season exists and is selected, display Add New Episode link
					
					echo '<p class="add"><a href="add_episode.php?s=' . $show . '&amp;t=' . $season . '">'._("Add New Episode").'</a></p>
					<p class="get"><a href="get_episodes.php?s=' . $show . '&amp;t=' . $season . '">'._("Import Episodes").'</a></p><p>';
				}
				else {
					echo '<p class="info3">'._("Select a season to add new episodes").'.</p>';
				}
				
				
				echo '<ul>';
						
						

						if ($has_season > 0) { // If a Season exists and is selected, show it's options
							echo '<li class="left_season">'._("Season Options").'</li><li class="mark_all_seen"><a href="mark_all_seen.php?s=' . $show . '&amp;t=' . $season . '">'._("Mark All as Seen").'</a></li>
									<li class="add_subs"><a href="add_subs_all.php?s=' . $show . '&amp;t=' . $season . '">'._("Add Subtitles to All").'</a></li>
									<li class="edit"><a href="edit_season.php?s=' . $show . '&amp;t=' . $season . '">'._("Edit this Season").'</a></li>
									<li class="delete"><a class="delete_season" href="delete_season.php?s=' . $show . '&amp;t=' . $season . '">'._("Delete this Season").'</a></li>';
						}		
				echo '	<li class="left_show">'._("Show Options").'</li>
						<li class="edit"><a href="edit_show.php?s=' . $show . '">'._("Edit this Show").'</a></li>
						<li class="delete"><a class="delete_show" href="delete_show.php?s=' . $show . '">'._("Delete this Show").'</a></li>
						</ul>
				</div>
				';
			
			echo '</div>
			';

			mysql_free_result($result);
			include("library/closedb.php");
	
			if ($season != null) {}
			else { // If there is no Season selected...
				echo '<div id="episode_list">
				<h3>'._("Seasons").':</h3>';
				
				include("library/config.php");
				include("library/opendb.php");
		
				$query="SELECT id, show_ID, season FROM seasons WHERE show_ID='$show' ORDER BY season ASC";
				$result = mysql_query($query);
				$results=mysql_numrows($result);
				
				// Show a list with all Seasons and total of episodes (and unseen episodes) per season
				if ($results == "0") {
					echo '<p class="erro">'._("This show has no seasons yet.").' <a href="add_season.php?s=' . $show . '">'._("Would you like to add a season now?").'</a></p>';
				}
				else {
					echo '<ul class="seasons_list">';
					$oddrow = 'odd';
					while(list($season_id,$show_id,$season_name)= mysql_fetch_row($result)) {
						echo '<li class="' . $oddrow . '">';
						$query2="SELECT * FROM episodes WHERE show_ID='$show' AND season_ID='$season_name'";
						$result2 = mysql_query($query2);
						$num=mysql_numrows($result2);
					
						$query3="SELECT * FROM episodes WHERE show_ID='$show' AND season_ID='$season_name' AND seen='0'";
						$result3 = mysql_query($query3);
						$num2=mysql_numrows($result3);
			
						echo '<a href="index.php?s=' . $show_id . '&amp;t=' . $season_name . '">'._("Season").' ' . $season_name . '</a> ' . $num . ' '._("Episodes").' <span';
						if ($num2 > "0") { // Change text color if there are unseen episodes
							echo ' class="are_unseen"';
						}
						echo'>(' . $num2 . ' '._("Unseen").')</span></li>
						';
					
						if ('odd' == $oddrow) $oddrow = 'even';
						else $oddrow = 'odd';
					}
					echo '</ul>
					';
				}
				
				if ($cast != '') {
					echo '<div id="cast">
					<h3>'._("Cast").':</h3>
					<ul>';
					echo $cast;
					echo '<p class="get_cast"><a href="update_cast.php?s=' . $show . '">'._("Update cast and show information").'</a></p>
					</div>
					';
				}
				else {
					echo '<p class="get_cast"><a href="update_cast.php?s=' . $show . '">'._("Update cast and show information").'</a></p>
					';
				}

				mysql_free_result($result);
				include("library/closedb.php");
			
			echo'</div>
			';
		}
		}
		else {
			echo '<div id="messages"><p class="erro">'._("There is no Show with that name on the database").'! <a href="index.php">'._("Back to the homepage").'</a>.</p>
			</div>
			';
		}
	} 

	if ($season != null) { // If a Season is selected, show all of it's episodes
		
		include("library/config.php");
		include("library/opendb.php");

		$query="SELECT show_ID, season_ID, episode, title, description, seen, subs FROM episodes WHERE show_ID='$show' AND season_ID='$season' ORDER BY (episode+0) ASC";
		$result = mysql_query($query);
		$num=mysql_numrows($result);
		
		$query2="SELECT * FROM seasons WHERE show_ID='$show' AND season='$season'";
		$result2 = mysql_query($query2);
		$has_season=mysql_numrows($result2);
		
		if ($has_season > 0) {
		// Show a list with all Seasons and total of episodes (and unseen episodes) per season
		if ($num == "0") {
			echo '<div id="episode_list">
			<p class="erro">'._("This season has no episodes yet.").' <a href="get_episodes.php?s=' . $show . '&amp;t=' . $season . '">'._("Would you like to add an episode now?").'</a></p>
			</div>
			';
		}
		else {
	
			echo '<div id="episode_list">';
			$oddrow = 'odd';
			while(list($show_id,$season_id,$episode_nr,$title,$description,$seen,$subs)= mysql_fetch_row($result)) {
			
				echo '<dl class="' ,$oddrow, '';
					if ($episode == $episode_nr) { // Change class if episode is selected
						echo ' selected ';
					}
					else {
						echo ' ep ';
					}
				echo '">';
		
				echo '<dt><span class="nr">' . $episode_nr . '</span> <span class="title';
						if ($seen == "0") { // Check to see is the episode is unseen
							echo ' unseen';
						}
					echo '">' . $title . '</span> <span class="';
					if ($subs == "1") { // Check to see is episode has subtitles
						echo 'subsok';
					}
					else {
						echo 'subsno';
					}
				echo '">&nbsp;</span></dt>
					<dd><p>' . $description . '</p>
					<div class="options">
						<p class="left">';
				
						if ($seen == "0") { // If episode is unseen...
							echo '<span><a href="mark_seen.php?s=' . $show_id . '&amp;t=' . $season . '&amp;e=' . $episode_nr . '">'._("Mark as Seen").'</a></span>';
						}
						else { // If episode is seen...
							echo '<span><a href="mark_unseen.php?s=' . $show_id . '&amp;t=' . $season . '&amp;e=' . $episode_nr . '">'._("Mark as Unseen").'</a></span> ';
						}
				
						if ($subs == "1") { // If episode has subtitles...
							echo ' <span><a href="remove_subs.php?s=' . $show_id . '&amp;t=' . $season . '&amp;e=' . $episode_nr . '">'._("Remove Subtitles").'</a></span>';
						}
						else { // If episode doesn't have subtitles...
							echo ' <span><a href="add_subs.php?s=' . $show_id . '&amp;t=' . $season . '&amp;e=' . $episode_nr . '">'._("Add Subtitles").'</a></span>';
						}
				
					echo '</p>
					<p class="right"><a class="edit" href="edit_episode.php?s=' . $show_id . '&amp;t=' . $season . '&amp;e=' . $episode_nr . '">'._("Edit").'</a> | <a class="delete delete_episode" href="delete_episode.php?s=' . $show_id . '&amp;t=' . $season . '&amp;e=' . $episode_nr . '">'._("Delete").'</a></p></div>
					</dd>
					';
	
				echo '</dl>
				';
	
				if ('odd' == $oddrow) $oddrow = 'even';
				else $oddrow = 'odd';
			}
	
			$query2="SELECT show_ID, season_ID, episode, title, description, seen, subs FROM episodes WHERE show_ID='$show' AND season_ID='$season' AND seen='0'";
			$result2 = mysql_query($query2);
			$num2=mysql_numrows($result2);
	
			// Show info about the number of episodes in this Season
			echo '<p class="episode_nrs">' . $num . ' '._("Episodes").' <span';
				if ($num2 > "0") { // Change text color is there are unseen episodes
					echo ' class="are_unseen"';
				}
			echo '>(' . $num2 . ' '._("Unseen").')</span></p>
			';
	
			mysql_free_result($result);
			include("library/closedb.php");
	
		echo '</div>
		';
		}
		}
		else {
			echo '<div id="episode_list">
				<p class="erro">'._("You don't have season").' ' . $season . ' '._("in you database").'. <a href="add_season.php?s=' . $show . '&amp;t=' . $season . '">'._("Would you like to add it now?").'</a></p>
				</div>';
		}
		
	}
?>
	
	<div class="push"></div>
	
</div><!-- /#main -->
<?php include("library/footer.php");?>
</body>
</html>